*TITLE: HOW DO VOTERS HOLD POLITICIANS ACCOUNTABLE FOR PERSONAL WELFARE? EVIDENCE OF A SELF-SERVING BIAS
*JOURNAL: JOURNAL OF POLITICS
*AUTHOR: MARTIN VINÆS LARSEN
*FILE: STUDY 1 ANALYSES (FIGURE 1, 2 & APPENDIX TABLES/FIGURES)
*DEPENDENCIES: eststo, blindschemes


///A) ANES DATA///

*loading dataset
use usrepli.dta, replace
label var gov "%noshow" //this is presidential vote

*estimate models
eststo f1: reg gov c.jointego##c.jointsoc  female educ age c.ideo i.year, r
drop if e(sample)==0
eststo a: reg gov jointego jointsoc 1.ego 1.socio  i.year , r
eststo a1: reg gov i.ego i.socio i.year, r
eststo b: reg gov jointego jointsoc 1.ego 1.socio female  c.ideo educ age i.year, r
eststo b1: reg gov i.ego i.socio  female age educ c.ideo i.year, r
eststo c1: logit gov i.ego i.socio i.year
eststo d1: logit gov i.ego i.socio  female educ c.ideo age i.year, r
eststo e1: reg gov c.jointego##c.jointsoc i.year

*export tables for appendix

*LPM
esttab a1 b1  using "apdxfullanes.tex", replace keep(1.ego 2.ego 1.socio 2.socio female educ ideology age) star(+ 0.10 * 0.05) b(%9.2fc) ///
stats(rmse r2 N, label(" $\sigma$" "R$^2$" "Observations") fmt(%9.2f %9.2f %9.0fc) ) nomtitles ///
varlabels(female "Female (ref:male)" 1.ego "Worse off - own economy"  ///
2.ego "Better off - own economy" 1.socio "Worse off - national economy" 2.socio "Better off - own economy" educ "Some college ore more (ref: none)" ideology "Ideology" age "Age" ) ///
label se  title(OLS regression of voting for party in government (ANES)} \footnotesize \label{table:anes) ///
addnotes("Dummies for election surveys included in all models." )

*logit
esttab c1 d1 using "apdxfullaneslogit.tex", replace keep(1.ego 2.ego 1.socio 2.socio female educ age ideology) star(+ 0.10 * 0.05) b(%9.2fc) ///
stats(r2_p N, label("Pseudo R$^2$" "Observations") fmt(%9.2f %9.0fc) ) nomtitles ///
varlabels(female "Female (ref:male)" 1.ego "Worse off - own economy"  ///
2.ego "Better off - own economy" 1.socio "Worse off - national economy" 2.socio "Better off - own economy" educ "Some college ore more (ref: none)" ideology "Ideology" age "Age" ) ///
label se  title(Logistic regression of voting for party in government (ANES)} \footnotesize \label{table:lanes) ///
addnotes("Dummies for election surveys included in all models." )

*interaction models
esttab e1 f1 using "apdxfullanesinter.tex", replace keep(jointego jointsoc c.jointego#c.jointsoc female age educ ideology) star(+ 0.10 * 0.05) b(%9.2fc) ///
stats(rmse r2 N, label(" $\sigma$" "R$^2$" "Observations") fmt(%9.2f %9.2f %9.0fc) ) nomtitles ///
varlabels(female "Female (ref:male)" 1.ego "Worse off - own economy"  ///
jointego "Own Econmy" jointsoc  "National economy" c.jointego#c.jointsoc "Interaction" educ "Some college ore more (ref: none)" ideology "Ideology" age "Age") ///
label se  title(Interaction model of voting for party in government (ANES)} \footnotesize \label{table:ianes) ///
addnotes("Dummies for election surveys included in all models." )


*descriptive statistics
drop if e(sample)==0
la var gov "Vote for presidential party"
la var age "Age"
la var female "Woman (ref: man)"
la var educ "Some college"
la var soc "State of country's economy"
la var ego "State of personal economy"
la var ideology "Ideology"
file open anyname using "desanes.txt", write text replace 
file write anyname  _newline  _col(0)  "\begin{table} [htbp] \centering \caption{Descriptive statistics, ANES\label{desdnes}} \footnotesize \begin{tabular}{l*{6}{c}}\hline\hline "
file write anyname _newline _col(0) "&Mean & SD & Min & Median & Max & n\\ \hline"
foreach x of var  gov ideology ego soc educ age female  {
su  `x' , d
file write anyname  _newline  _col(0) (`"`: var label `x''"') "&" _col(25) %9.2f  (r(mean)) " &" _col(45) %9.2f  (r(sd)) " &" _col(65) %9.2f  (r(min)) " &"   _col(85) %9.2f  (r(p50)) " &" _col(105) %9.2f  (r(max)) " &" _col(125) %9.0f  (r(N)) " \\"
}
file write anyname _newline _col(0) "\hline\hline"
file write anyname _newline _col(0) "\end{tabular}"
file write anyname _newline _col(0) "\end{table}"
file close anyname

*ANES part of figure 1
tempfile graphanes
postfile results id controls b se using `graphanes', replace
foreach x in a b {
estimates restore `x'1
local controls=0
if  "`x'"=="b" {
local controls=1
}
post results (1) (`controls') (_b[1.socio]) (_se[1.socio])
post results (2) (`controls') (_b[2.socio]) (_se[2.socio])
post results (4) (`controls') (_b[1.ego]) (_se[1.ego])
post results (5) (`controls') (_b[2.ego]) (_se[2.ego])
estimates restore `x'
post results (3) (`controls') (_b[1.socio]*-1) (_se[1.socio])
post results (6) (`controls') (_b[1.ego]*-1) (_se[1.ego])
}
postclose results
use `graphanes', clear
drop if controls==1
gen ub90=b+se*1.64
gen ub95=b+se*1.96
gen lb90=b-se*1.64
gen lb95=b-se*1.96
replace id=id+1 if id > 3
twoway  rspike ub90 lb90 id, horizontal lwidth(thick) lcolor(black*0.7)  ///
|| rspike ub95 lb95 id, horizontal lwidth(medthick) lcolor(black*0.8)  ///
|| scatter id b if id > 4, msym(O) msize(large) mcolor(black) ///
|| scatter id b if id < 4, msym(O) msize(large) mcolor(white) mlwidth(medthick) mlcolor(black) ///
scheme(plotplain) xtick(-0.2(0.05)0.2)  xline(0, lpattern(dash)) xtitle(" " "Effect on Incumbent Support", size(vlarge)) ///
xlabel(, labsize(vlarge)) yscale(reverse) ylab(-1 " " 1 "Worse   " 2 "Better   " 3 "Valence Asymmetry   " 5 "Worse   " 6 "Better   " 7 "Valence Asymmetry   " 8 " ", angle(0) nogrid notick labsize(vlarge)) ///
ytitle(" ") legend(style(background) cols(1) position(1) ring(0) bmargin(tiny) title(ANES, size(vlarge)) ///
order(4 3) size(vlarge) label(1 "90 pct. CI") label(2 "95 pct. CI") label(3 "Own economy") label(4 "Ntnl economy") region(lwidth(none))) xsize(7)
graph export "elecstudy1.pdf", replace






///B) BES DATA///

*importing data
use ukrepli.dta, replace
label var gov "%noshow"

*estimating models
eststo f1: reg gov c.jointego##c.jointsoc  female age educ c.ideo age i.year, r
drop if e(sample)==0
eststo a: reg gov jointego jointsoc 1.ego 1.socio  i.year , r
eststo a1: reg gov i.ego i.socio i.year, r
eststo b: reg gov jointego jointsoc 1.ego 1.socio female educ c.ideo   i.year, r
eststo b1: reg gov i.ego i.socio  female  educ age c.ideo i.year, r
eststo c1: logit gov i.ego i.socio i.year
eststo d1: logit gov i.ego i.socio  female educ c.ideo i.year age, r
eststo e1: reg gov c.jointego##c.jointsoc i.year
eststo f1: reg gov c.jointego##c.jointsoc  female educ c.ideo age i.year, r

*estimating models with PM party and government party DV's
tempfile govuk pmuk
reg gov jointego jointsoc 1.ego 1.socio female educ c.ideo   i.year, r
margins, dydx(ego) saving(`govuk')
reg pm jointego jointsoc 1.ego 1.socio female educ c.ideo   i.year, r
margins, dydx(ego)
margins, dydx(ego) saving(`pmuk')

*tables for appendix

*LPM
esttab a1 b1  using "apdxfullbes.tex", replace keep(1.ego 2.ego 1.socio 2.socio female educ ideology age) star(+ 0.10 * 0.05) b(%9.2fc) ///
stats(rmse r2 N, label(" $\sigma$" "R$^2$" "Observations") fmt(%9.2f %9.2f %9.0fc) ) nomtitles ///
varlabels(female "Female (ref:male)" 1.ego "Worse off - own economy"  ///
2.ego "Better off - own economy" 1.socio "Worse off - national economy" 2.socio "Better off - own economy" educ "Some college ore more (ref: none)" ideology "Ideology" age "Age" ) ///
label se  title(OLS regression of voting for party in government (BES)} \footnotesize \label{table:bes) ///
addnotes("Dummies for election surveys included in all models." )

*logit
esttab c1 d1 using "apdxfullbeslogit.tex", replace keep(1.ego 2.ego 1.socio 2.socio female educ ideology age) star(+ 0.10 * 0.05) b(%9.2fc) ///
stats(r2_p N, label("Pseudo R$^2$" "Observations") fmt(%9.2f %9.0fc) ) nomtitles ///
varlabels(female "Female (ref:male)" 1.ego "Worse off - own economy"  ///
2.ego "Better off - own economy" 1.socio "Worse off - national economy" 2.socio "Better off - own economy" educ "Some college ore more (ref: none)" ideology "Ideology" age "Age") ///
label se  title(Logistic regression of voting for party in government (BES)} \footnotesize \label{table:lbes) ///
addnotes("Dummies for election surveys included in all models." )

*interaction models
esttab e1 f1 using "apdxfullbesinter.tex", replace keep(jointego jointsoc c.jointego#c.jointsoc female educ ideology ) star(+ 0.10 * 0.05) b(%9.2fc) ///
stats(rmse r2 N, label(" $\sigma$" "R$^2$" "Observations") fmt(%9.2f %9.2f %9.0fc) ) nomtitles ///
varlabels(female "Female (ref:male)" 1.ego "Worse off - own economy"  ///
jointego "Own Econmy" jointsoc  "National economy" c.jointego#c.jointsoc "Interaction" educ "Some college ore more (ref: none)" ideology "Ideology" age "Age") ///
label se  title(Interaction model of voting for party in government (BES)} \footnotesize \label{table:ibes) ///
addnotes("Dummies for election surveys included in all models." )

*descriptive statistics
drop if e(sample)==0
la var gov "Vote for party in government"
la var pm "Vote for prime minister party"
la var age "Age"
la var female "Woman (ref: man)"
la var educ "Some college"
la var soc "State of country's economy"
la var ego "State of personal economy"
la var ideology "Ideology"

file open anyname using "desbes.txt", write text replace 
file write anyname  _newline  _col(0)  "\begin{table} [htbp] \centering \caption{Descriptive statistics, BES\label{desdnes}} \footnotesize \begin{tabular}{l*{6}{c}}\hline\hline "
file write anyname _newline _col(0) "&Mean & SD & Min & Median & Max & n\\ \hline"
foreach x of var  gov pm ideology ego soc educ age female {
su  `x' , d
file write anyname  _newline  _col(0) (`"`: var label `x''"') "&" _col(25) %9.2f  (r(mean)) " &" _col(45) %9.2f  (r(sd)) " &" _col(65) %9.2f  (r(min)) " &"   _col(85) %9.2f  (r(p50)) " &" _col(105) %9.2f  (r(max)) " &" _col(125) %9.0f  (r(N)) " \\"
}
file write anyname _newline _col(0) "\hline\hline"
file write anyname _newline _col(0) "\end{tabular}"
file write anyname _newline _col(0) "\end{table}"
file close anyname

*BES part of figure 1
tempfile graphdatbes
postfile results id controls b se using `graphdatbes', replace
foreach x in a b {
estimates restore `x'1
local controls=0
if  "`x'"=="b" {
local controls=1
}
post results (1) (`controls') (_b[1.soc]) (_se[1.soc])
post results (2) (`controls') (_b[2.soc]) (_se[2.soc])
post results (4) (`controls') (_b[1.ego]) (_se[1.ego])
post results (5) (`controls') (_b[2.ego]) (_se[2.ego])
estimates restore `x'
post results (3) (`controls') (_b[1.soc]*-1) (_se[1.soc])
post results (6) (`controls') (_b[1.ego]*-1) (_se[1.ego])
}
postclose results
use `graphdatbes', clear
drop if controls==1
gen ub90=b+se*1.64
gen ub95=b+se*1.96
gen lb90=b-se*1.64
gen lb95=b-se*1.96
replace id=id+1 if id > 3
drop if id >7
twoway  rspike ub90 lb90 id, horizontal lwidth(thick) lcolor(black*0.7)  ///
|| rspike ub95 lb95 id, horizontal lwidth(medthick) lcolor(black*0.8)  ///
|| scatter id b if id > 4, msym(O) msize(large) mcolor(black) ///
|| scatter id b if id < 4, msym(O) msize(large) mcolor(white) mlwidth(medthick) mlcolor(black) ///
scheme(plotplain) xtick(-0.2(0.05)0.2) xline(0, lpattern(dash)) xtitle(" " "Effect on Incumbent Support", size(vlarge)) ///
yscale(reverse) xlabel(, labsize(vlarge)) ylab(-1 " " 1 "Worse   " 2 "Better   " 3 "Valence Asymmetry   " 5 "Worse   " 6 "Better   " 7 "Valence Asymmetry   " 8 " ", angle(0) nogrid notick labsize(vlarge)) ///
ytitle(" ") legend(style(background) cols(1) position(1) ring(0) bmargin(tiny) title(BES, size(vlarge)) ///
order(4 3) size(vlarge) label(1 "90 pct. CI") label(2 "95 pct. CI") label(3 "Own economy") label(4 "Ntnl economy") region(lwidth(none))) xsize(7)
graph export "elecstudy5.pdf", replace



///C) DNES DATA///

*importing data
use dkrepli.dta, replace
label var gov "%noshow"

*estimating models
eststo f1: reg gov c.jointego##c.jointsoc  female age educ c.ideo i.year, r
drop if e(sample)==0
eststo a: reg gov jointego jointsoc 1.ego 1.socio  i.year , r
eststo a1: reg gov i.ego i.socio i.year, r
eststo b: reg gov jointego jointsoc 1.ego 1.socio female educ c.ideo i.year, r
eststo b1: reg gov i.ego i.socio  female age educ c.ideo i.year, r
eststo c1: logit gov i.ego i.socio i.year
eststo d1: logit gov i.ego i.socio  female age educ c.ideo i.year, r
eststo e1: reg gov c.jointego##c.jointsoc i.year
eststo f1: reg gov c.jointego##c.jointsoc  female age educ c.ideo i.year, r

*estimating models with PM party and Government party DV's
tempfile govdk pmdk
reg gov jointego jointsoc 1.ego 1.socio female educ c.ideo   i.year, r
margins, dydx(ego) saving(`govdk')
reg pm jointego jointsoc 1.ego 1.socio female educ c.ideo   i.year, r
margins, dydx(ego) saving(`pmdk')

*appendix tables

*LPM
esttab a1 b1  using "apdxfulldnes.tex", replace keep(1.ego 2.ego 1.socio 2.socio female educ ideology age) star(+ 0.10 * 0.05) b(%9.2fc) ///
stats(rmse r2 N, label(" $\sigma$" "R$^2$" "Observations") fmt(%9.2f %9.2f %9.0fc) ) nomtitles ///
varlabels(female "Female (ref:male)" 1.ego "Worse off - own economy"  ///
2.ego "Better off - own economy" 1.socio "Worse off - national economy" 2.socio "Better off - own economy" educ "Some college ore more (ref: none)" ideology "Ideology" age "Age") ///
label se  title(OLS regression of voting for party in government (DNES)} \footnotesize \label{table:dnes) ///
addnotes("Dummies for election surveys included in all models." )

*logit
esttab c1 d1 using "apdxfulldneslogit.tex", replace keep(1.ego 2.ego 1.socio 2.socio female educ ideology age) star(+ 0.10 * 0.05) b(%9.2fc) ///
stats(r2_p N, label("Pseudo R$^2$" "Observations") fmt(%9.2f %9.0fc) ) nomtitles ///
varlabels(female "Female (ref:male)" 1.ego "Worse off - own economy"  ///
2.ego "Better off - own economy" 1.socio "Worse off - national economy" 2.socio "Better off - own economy" educ "Some college ore more (ref: none)" ideology "Ideology" age "Age") ///
label se  title(Logistic regression of voting for party in government (DNES)} \footnotesize \label{table:ldnes) ///
addnotes("Dummies for election surveys included in all models." )

*interaction models
esttab e1 f1 using "apdxfulldnesinter.tex", replace keep(jointego jointsoc c.jointego#c.jointsoc female educ ideology age) star(+ 0.10 * 0.05) b(%9.2fc) ///
stats(rmse r2 N, label(" $\sigma$" "R$^2$" "Observations") fmt(%9.2f %9.2f %9.0fc) ) nomtitles ///
varlabels(female "Female (ref:male)" 1.ego "Worse off - own economy"  ///
jointego "Own Econmy" jointsoc  "National economy" c.jointego#c.jointsoc "Interaction" educ "Some college ore more (ref: none)" ideology "Ideology" age "Age") ///
label se  title(Interaction model of voting for party in government (DNES)} \footnotesize \label{table:idnes) ///
addnotes("Dummies for election surveys included in all models." )

*descriptive statistics
drop if e(sample)==0
la var gov "Vote for party in government"
la var pm "Vote for prime minister party"
la var age "Age"
la var female "Woman (ref: man)"
la var educ "Some college"
la var soc "State of country's economy"
la var ego "State of personal economy"
la var ideology "Ideology"
file open anyname using "desdnes.txt", write text replace 
file write anyname  _newline  _col(0)  "\begin{table} [htbp] \centering \caption{Descriptive statistics, DNES\label{desdnes}} \footnotesize \begin{tabular}{l*{6}{c}}\hline\hline "
file write anyname _newline _col(0) "&Mean & SD & Min & Median & Max & n\\ \hline"
foreach x of var  gov pm ideology ego soc educ age female  {
su  `x' , d
file write anyname  _newline  _col(0) (`"`: var label `x''"') "&" _col(25) %9.2f  (r(mean)) " &" _col(45) %9.2f  (r(sd)) " &" _col(65) %9.2f  (r(min)) " &"   _col(85) %9.2f  (r(p50)) " &" _col(105) %9.2f  (r(max)) " &" _col(125) %9.0f  (r(N)) " \\"
}
file write anyname _newline _col(0) "\hline\hline"
file write anyname _newline _col(0) "\end{tabular}"
file write anyname _newline _col(0) "\end{table}"
file close anyname


*DNES part of figure 1
tempfile graphdatdnes
postfile results id controls b se using `graphdatdnes', replace
foreach x in a b {
estimates restore `x'1
local controls=0
if  "`x'"=="b" {
local controls=1
}
post results (1) (`controls') (_b[1.soc]) (_se[1.soc])
post results (2) (`controls') (_b[2.soc]) (_se[2.soc])
post results (4) (`controls') (_b[1.ego]) (_se[1.ego])
post results (5) (`controls') (_b[2.ego]) (_se[2.ego])
estimates restore `x'
post results (3) (`controls') (_b[1.soc]*-1) (_se[1.soc])
post results (6) (`controls') (_b[1.ego]*-1) (_se[1.ego])
}
postclose results
use `graphdatdnes', clear
drop if controls==1
gen ub90=b+se*1.64
gen ub95=b+se*1.96
gen lb90=b-se*1.64
gen lb95=b-se*1.96
replace id=id+1 if id > 3
drop if id >7
twoway  rspike ub90 lb90 id, horizontal lwidth(thick) lcolor(black*0.7)  ///
|| rspike ub95 lb95 id, horizontal lwidth(medthick) lcolor(black*0.8)  ///
|| scatter id b if id > 4, msym(O) msize(large) mcolor(black) ///
|| scatter id b if id < 4, msym(O) msize(large) mcolor(white) mlwidth(medthick) mlcolor(black) ///
scheme(plotplain) xtick(-0.2(0.05)0.2) xline(0, lpattern(dash)) xtitle(" " "Effect on Incumbent Support", size(vlarge)) ///
yscale(reverse) xlabel(, labsize(vlarge)) ylab(-1 " " 1 "Worse   " 2 "Better   " 3 "Valence Asymmetry   " 5 "Worse   " 6 "Better   " 7 "Valence Asymmetry   " 8 " ", angle(0) nogrid notick labsize(vlarge)) ///
ytitle(" ") legend(style(background) cols(1) position(1) ring(0) bmargin(tiny) title(DNES, size(vlarge)) ///
order(4 3) size(vlarge) label(1 "90 pct. CI") label(2 "95 pct. CI") label(3 "Own economy") label(4 "Ntnl economy") region(lwidth(none))) xsize(7)
graph export "elecstudy2.pdf", replace




///D) AusES DATA///

*import data
use ausrepli.dta, replace
label var gov "%noshow"

*estimating models
eststo f1: reg gov c.jointego##c.jointsoc female age educ c.ideo  i.year, r
drop if e(sample)==0
eststo a: reg gov jointego jointsoc 1.ego 1.socio  i.year , r
eststo b: reg gov jointego jointsoc 1.ego 1.socio female age educ c.ideo    i.year, r
eststo a1: reg gov i.ego i.socio i.year, r
eststo b1: reg gov i.ego i.socio  female  educ c.ideo age  i.year, r
eststo c1: logit gov i.ego i.socio i.year
eststo d1: logit gov i.ego i.socio  female  educ c.ideo age i.year, r
eststo e1: reg gov c.jointego##c.jointsoc i.year
eststo f1: reg gov c.jointego##c.jointsoc female educ c.ideo  age i.year, r

*estimating models with PM party and Government party DV's
tempfile govaus pmaus
reg gov jointego jointsoc 1.ego 1.socio female educ c.ideo   i.year, r
margins, dydx(ego) saving(`govaus')
reg pm jointego jointsoc 1.ego 1.socio female educ c.ideo   i.year, r
margins, dydx(ego) saving(`pmaus')

*appendix tables

*LPM
esttab a1 b1  using "apdxfullauses.tex", replace keep(1.ego 2.ego 1.socio 2.socio female educ age ideology) star(+ 0.10 * 0.05) b(%9.2fc) ///
stats(rmse r2 N, label(" $\sigma$" "R$^2$" "Observations") fmt(%9.2f %9.2f %9.0fc) ) nomtitles ///
varlabels(female "Female (ref:male)" 1.ego "Worse off - own economy"  ///
2.ego "Better off - own economy" 1.socio "Worse off - national economy" 2.socio "Better off - own economy" educ "Some college ore more (ref: none)" ideology "Ideology" age "Age") ///
label se  title(OLS regression of voting for party in government (AusES)} \footnotesize \label{table:ausnes) ///
addnotes("Dummies for election surveys included in all models." )

*logit
esttab c1 d1 using "apdxfullauseslogit.tex", replace keep(1.ego 2.ego 1.socio 2.socio female educ ideology age) star(+ 0.10 * 0.05) b(%9.2fc) ///
stats(r2_p N, label("Pseudo R$^2$" "Observations") fmt(%9.2f %9.0fc) ) nomtitles ///
varlabels(female "Female (ref:male)" 1.ego "Worse off - own economy"  ///
2.ego "Better off - own economy" 1.socio "Worse off - national economy" 2.socio "Better off - own economy" educ "Some college ore more (ref: none)" ideology "Ideology" age "Age" ) ///
label se  title(Logistic regression of voting for party in government (AusES)} \footnotesize \label{table:lausnes) ///
addnotes("Dummies for election surveys included in all models." )

*interaction models
esttab e1 f1 using "apdxfullausesinter.tex", replace keep(jointego jointsoc c.jointego#c.jointsoc female educ ideology age) star(+ 0.10 * 0.05) b(%9.2fc) ///
stats(rmse r2 N, label(" $\sigma$" "R$^2$" "Observations") fmt(%9.2f %9.2f %9.0fc) ) nomtitles ///
varlabels(female "Female (ref:male)" 1.ego "Worse off - own economy"  ///
jointego "Own Econmy" jointsoc  "National economy" c.jointego#c.jointsoc "Interaction" educ "Some college ore more (ref: none)" ideology "Ideology" age "Age") ///
label se  title(Interaction model of voting for party in government (AusES)} \footnotesize \label{table:iauses) ///
addnotes("Dummies for election surveys included in all models." )

*descriptive statistics
drop if e(sample)==0
la var gov "Vote for party in government"
la var pm "Vote for prime minister party"
la var age "Age"
la var female "Woman (ref: man)"
la var educ "Some college"
la var soc "State of country's economy"
la var ego "State of personal economy"
la var ideology "Ideology"

file open anyname using "desauses.txt", write text replace 
file write anyname  _newline  _col(0)  "\begin{table} [htbp] \centering \caption{Descriptive statistics, AusES\label{desdnes}} \footnotesize \begin{tabular}{l*{6}{c}}\hline\hline "
file write anyname _newline _col(0) "&Mean & SD & Min & Median & Max & n\\ \hline"
foreach x of var  gov pm ideology ego soc educ age female  {
su  `x' , d
file write anyname  _newline  _col(0) (`"`: var label `x''"') "&" _col(25) %9.2f  (r(mean)) " &" _col(45) %9.2f  (r(sd)) " &" _col(65) %9.2f  (r(min)) " &"   _col(85) %9.2f  (r(p50)) " &" _col(105) %9.2f  (r(max)) " &" _col(125) %9.0f  (r(N)) " \\"
}
file write anyname _newline _col(0) "\hline\hline"
file write anyname _newline _col(0) "\end{tabular}"
file write anyname _newline _col(0) "\end{table}"
file close anyname

*AusES part of figure 1
tempfile graphdataus
postfile results id controls b se using `graphdataus', replace
foreach x in a b {
estimates restore `x'1
local controls=0
if  "`x'"=="b" {
local controls=1
}
post results (1) (`controls') (_b[1.socio]) (_se[1.socio])
post results (2) (`controls') (_b[2.socio]) (_se[2.socio])
post results (4) (`controls') (_b[1.ego]) (_se[1.ego])
post results (5) (`controls') (_b[2.ego]) (_se[2.ego])
estimates restore `x'
post results (3) (`controls') (_b[1.socio]*-1) (_se[1.socio])
post results (6) (`controls') (_b[1.ego]*-1) (_se[1.ego])
}
postclose results

use `graphdataus', clear
drop if controls==1
gen ub90=b+se*1.64
gen ub95=b+se*1.96
gen lb90=b-se*1.64
gen lb95=b-se*1.96
replace id=id+1 if id > 3
twoway  rspike ub90 lb90 id, horizontal lwidth(thick) lcolor(black*0.7)  ///
|| rspike ub95 lb95 id, horizontal lwidth(medthick) lcolor(black*0.8)  ///
|| scatter id b if id > 4, msym(O) msize(large) mcolor(black) ///
|| scatter id b if id < 4, msym(O) msize(large) mcolor(white) mlwidth(medthick) mlcolor(black) ///
scheme(plotplain) xtick(-0.2(0.05)0.2)  xline(0, lpattern(dash)) xtitle(" " "Effect on Incumbent Support", size(vlarge)) ///
yscale(reverse) ylab(-1 " " 1 "Worse   " 2 "Better   " 3 "Valence Asymmetry   " 5 "Worse   " 6 "Better   " 7 "Valence Asymmetry   " 8 " ", angle(0) notick labsize(vlarge) nogrid) ///
xlabel(, labsize(vlarge)) ytitle(" ") legend(style(background) cols(1) position(1) ring(0) bmargin(tiny) title(AusES, size(vlarge)) ///
order(4 3) size(vlarge) label(1 "90 pct. CI") label(2 "95 pct. CI") label(3 "Own economy") label(4 "Ntnl economy") region(lwidth(none))) xsize(7)
graph export "elecstudy3.pdf", replace


///E) LATINOBAROMETRO DATA///

*importing data
use latinrepli.dta, replace
label var approveprez "%noshow"

*estimating models
eststo f1: reg approveprez c.jointego##c.jointsocio  i.cntryyearnum female age educ c.ideo  i.cntryyearnum 
drop if e(sample)==0
eststo a: reg approveprez jointego jointsocio 1.egoretro 1.socioretro  i.cntryyearnum , r
eststo a1: reg approveprez i.egoretro i.socioretro  i.cntryyearnum , r
eststo b: reg approveprez jointego jointsocio 1.egoretro 1.socioretro female educ age  c.ideo  i.cntryyearnum, r
eststo b1: reg approveprez i.egoretro i.socioretro female age educ c.ideo  i.cntryyearnum , r
eststo c1: logit approveprez i.egoretro i.socioretro  i.cntryyearnum 
eststo d1: logit approveprez i.egoretro i.socioretro  i.cntryyearnum female educ age c.ideo  i.cntryyearnum 
eststo e1: reg approveprez c.jointego##c.jointsocio  i.cntryyearnum, r 

*appendix tables

*LPM
esttab a1 b1  using "apdxfulllatin.tex", replace keep(1.egoretro 2.egoretro 1.socioretro 2.socioretro female educ ideo age ) star(+ 0.10 * 0.05) b(%9.2fc) ///
stats(rmse r2 N, label(" $\sigma$" "R$^2$" "Observations") fmt(%9.2f %9.2f %9.0fc) ) nomtitles ///
varlabels(female "Female (ref:male)" 1.egoretro "Worse off - own economy"  ///
2.egoretro "Better off - own economy" 1.socioretro "Worse off - national economy" 2.socioretro "Better off - own economy" educ "Some college ore more (ref: none)" ideo "Ideology" age "Age") ///
label se  title(OLS regression of voting for party in government (Latinobar.)} \footnotesize \label{table:latin) ///
addnotes("Dummies for election surveys included in all models." )

*logit
esttab c1 d1 using "apdxfulllatinlogit.tex", replace keep(1.egoretro 2.egoretro 1.socioretro 2.socioretro female educ ideo age) star(+ 0.10 * 0.05) b(%9.2fc) ///
stats(r2_p N, label("Pseudo R$^2$" "Observations") fmt(%9.2f %9.0fc) ) nomtitles ///
varlabels(female "Female (ref:male)" 1.egoretro "Worse off - own economy"  ///
2.egoretro "Better off - own economy" 1.socioretro "Worse off - national economy" 2.socioretro "Better off - own economy" educ "Some college ore more (ref: none)" ideo "Ideology" age "Age") ///
label se  title(Logistic regression of voting for party in government (Latinobar.)} \footnotesize \label{table:llatin) ///
addnotes("Dummies for election surveys included in all models." )

*interaction models
esttab e1 f1 using "apdxfulllatininter.tex", replace keep(jointego jointsocio c.jointego#c.jointsocio female educ ideo age) star(+ 0.10 * 0.05) b(%9.2fc) ///
stats(rmse r2 N, label(" $\sigma$" "R$^2$" "Observations") fmt(%9.2f %9.2f %9.0fc) ) nomtitles ///
varlabels(female "Female (ref:male)" 1.ego "Worse off - own economy"  ///
jointego "Own Econmy" jointsocio  "National economy" c.jointego#c.jointsocio "Interaction" educ "Some college ore more (ref: none)" ideo "Ideology" age "Age") ///
label se  title(Interaction model of voting for party in government (Latinobar.)} \footnotesize \label{table:ilatin) ///
addnotes("Dummies for election surveys included in all models." )

*descriptive statistics
la var approveprez "Approve of president"
la var age "Age"
la var female "Woman (ref: man)"
la var educ "Some university"
la var socioret "State of country's economy"
la var egoret "State of personal economy"
la var ideo "Ideology"


file open anyname using "deslatino.txt", write text replace 
file write anyname  _newline  _col(0)  "\begin{table} [htbp] \centering \caption{Descriptive statistics, Latinobarometro\label{deslatino}} \footnotesize \begin{tabular}{l*{6}{c}}\hline\hline "
file write anyname _newline _col(0) "&Mean & SD & Min & Median & Max & n\\ \hline"
foreach x of var  approveprez ideo egoret socioret educ age female {
su  `x' , d
file write anyname  _newline  _col(0) (`"`: var label `x''"') "&" _col(25) %9.2f  (r(mean)) " &" _col(45) %9.2f  (r(sd)) " &" _col(65) %9.2f  (r(min)) " &"   _col(85) %9.2f  (r(p50)) " &" _col(105) %9.2f  (r(max)) " &" _col(125) %9.0f  (r(N)) " \\"
}
file write anyname _newline _col(0) "\hline\hline"
file write anyname _newline _col(0) "\end{tabular}"
file write anyname _newline _col(0) "\end{table}"
file close anyname

*Latinobarometro part of figure 1
tempfile graphdatlat
postfile results id controls b se using `graphdatlat', replace
foreach x in a b {
estimates restore `x'1
local controls=0
if  "`x'"=="b" {
local controls=1
}
post results (1) (`controls') (_b[1.socioretro]) (_se[1.socioretro])
post results (2) (`controls') (_b[2.socioretro]) (_se[2.socioretro])
post results (4) (`controls') (_b[1.egoretro]) (_se[1.egoretro])
post results (5) (`controls') (_b[2.egoretro]) (_se[2.egoretro])
estimates restore `x'
post results (3) (`controls') (_b[1.socioretro]*-1) (_se[1.socioretro])
post results (6) (`controls') (_b[1.egoretro]*-1) (_se[1.egoretro])
}
postclose results
use `graphdatlat', clear
drop if controls==1
gen ub90=b+se*1.64
gen ub95=b+se*1.96
gen lb90=b-se*1.64
gen lb95=b-se*1.96
replace id=id+1 if id > 3
twoway  rspike ub90 lb90 id, horizontal lwidth(thick) lcolor(black*0.7)  ///
|| rspike ub95 lb95 id, horizontal lwidth(medthick) lcolor(black*0.8)  ///
|| scatter id b if id > 4, msym(O) msize(large) mcolor(black) ///
|| scatter id b if id < 4, msym(O) msize(large) mcolor(white) mlwidth(medthick) mlcolor(black) ///
scheme(plotplain) xtick(-0.2(0.05)0.2)  xline(0, lpattern(dash)) xtitle(" " "Effect on Incumbent Support", size(vlarge)) ///
yscale(reverse) ylab(-1 " " 1 "Worse   " 2 "Better   " 3 "Valence Asymmetry   " 5 "Worse   " 6 "Better   " 7 "Valence Asymmetry   " 8 " ", angle(0) notick labsize(vlarge) nogrid) ///
xlabel(, labsize(vlarge)) ytitle(" ") legend(style(background) cols(1) position(1) ring(0) bmargin(tiny) title(Latinobarométro, size(vlarge)) ///
order(4 3) size(vlarge) label(1 "90 pct. CI") label(2 "95 pct. CI") label(3 "Own economy") label(4 "Ntnl economy") region(lwidth(none))) xsize(7)
graph export "elecstudy4.pdf", replace


///FIGURE 2///

*combining results from different election studies
use `graphanes', clear
gen var=1
append using `graphdatbes'
replace var=2 if var==.
append using `graphdatdnes'
replace var=3 if var==.
append using `graphdataus'
replace var=4 if var==.
append using `graphdatlat' 
replace var=5 if var==.
keep if id == 6 
replace id=id/3


*figure 2
gen ub90=b+se*1.64
gen ub95=b+se*1.96
gen lb90=b-se*1.64
gen lb95=b-se*1.96
replace id=id+3 if var==2
replace id=id+6 if var==3
replace id=id+9 if var==4
replace id=id+12 if var==5
replace id=id-0.5 if controls==0
replace id=id+0.5
gen roundb=round(b,0.001)
twoway rspike ub90 lb90 id, lwidth(thick) lcolor(black*0.8) horizontal|| ///
rspike ub95 lb95 id, lwidth(medthick)  lcolor(black*0.8) horizontal || ///
scatter id b if id ==2.5 | id==5.5 | id==8.5 | id==11.5 | id==14.5, mlab(roundb) mlabsiz(medlarge) mlabpos(5) msym(D) msize(large)  mcolor(white) mlwidth(medthick) mlcolor(black) || ///
scatter id b if id ==2 | id==5 | id==8 | id==11 | id==14, mlab(roundb) mlabsiz(medlarge) mlabpos(1) msize(large) msym(D) mcolor(black)  ///
scheme(plotplain) yscale(reverse)  xlabel(-0.05(0.025)0.15, labsize(vlarge)) xline(0, lpattern(dash)) ytitle(" ") ///
ylabel(0 " " 1.5 "The ANES" 4.5 "The BES" 7.5 "The DNES"  10.5 "AusES"  13.5 "Latinobarómetro" 15.5 " ", notick angle(0) labsize(vlarge) nogrid) xtitle(" " "Valence Asymmetry in Effect of Personal Economic Conditions", size(vlarge)) ///
legend(style(background) ring(0) cols(1) position(1) ///
order(4 3) size(vlarge) label(4 "No Controls") label(3 "Controls")  ///
title(All Surveys, size(vlarge))  region(lwidth(none) fcolor(none))  bmargin(none)) xsize(9)
graph export "controls.pdf", replace


//COMBINED APPENDIX FIGURE - PM ctr GOV//

*combining results from different election studies
use `pmaus', clear
append using `govaus'
append using `pmdk'
append using `govdk'
append using `pmuk'
append using `govuk'


*drawing figure
gen n=_n
recode n (1 3 5=1 "PM") (2 4 6=2 "Gov"), gen(dv)
recode n (2=1.2) (4=3.2) (6=5.2)
replace _margin=_margin*-1
replace _ci_lb=_ci_lb*-1
replace _ci_ub=_ci_ub*-1
gen ub90=_margin+_se*1.64
gen lb90=_margin-_se*1.64
twoway rspike _ci_lb _ci_ub  n, lwidth(medthick) horizontal scheme(plotplain) || ///
rspike ub90 lb90 n, lwidth(thick)  lcolor(black) horizontal || ///
scatter   n _margin if dv==1, xline(0) mlabsiz(medlarge) mlabpos(5) msym(D) msize(large)  mcolor(white) mlwidth(medthick) mlcolor(black)  || ///
scatter   n _margin if dv==2, mlabsiz(medlarge) mlabpos(1) msize(large) msym(D) mcolor(black)  ///
ylabel(1.1 "AusES" 3.1 "DNES" 5.1 "BES", labsize(large)) xlab(, labsize(large)) ytitle(" ") xtitle("Valence Asymmetry", size(large))  ///
legend(style(background) ring(1) cols(1) position(2) order(4 3) size(large) label(3 "Prime Minister") label(4 "Government"))
graph export "apdxpm.pdf", replace


********************************************************************************

